/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package com.ntb.controllers;

import com.ntb.common.PaginationUtil;
import com.ntb.common.SecuredAction;
import com.ntb.controllers.forms.LandListingForm;
import com.ntb.entities.Lands;
import com.ntb.models.LandPlansLaidDAO;
import com.ntb.models.LandStatusDAO;
import com.ntb.models.LandsDAO;
import com.ntb.models.LocationsDAO;
import com.ntb.models.impl.LandPlansLaidDAOImpl;
import com.ntb.models.impl.LandStatusDAOImpl;
import com.ntb.models.impl.LandsDAOImpl;
import com.ntb.models.impl.LocationsDAOImpl;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import org.apache.struts.action.ActionForm;
import org.apache.struts.action.ActionForward;
import org.apache.struts.action.ActionMapping;

/**
 *
 * @author Thinh Hung
 */
public class LandListingAction extends SecuredAction {

    @Override
    public ActionForward ntbExecute(ActionMapping mapping, ActionForm form, HttpServletRequest request, HttpServletResponse response) {

        LandListingForm landListingForm = (LandListingForm) form;

        LandsDAO landDAO = new LandsDAOImpl();

        String action = landListingForm.getAction();

        List<String> errorMessages = new ArrayList();
        List<String> successMessages = new ArrayList();
        boolean isHaveAction = true;
        if (("delete").equalsIgnoreCase(action)) {
            if (landDAO.delete(landListingForm.getLandID()) == 0) {
                errorMessages.add("There was an error deleting this land. You must delete all buildings associated with this land before deleting the land.");
            } else {
                landDAO.delete(landListingForm.getLandID());
                successMessages.add("1 lands out of 1 successfully deleted.");
            }
        } else if (("deleteLands").equalsIgnoreCase(action)) {
            int[] landIDs = landListingForm.getLandIDs();
            int count = 0;

            int length = 0;

            try {
                length = landIDs.length;
            } catch (Exception ex) {
            }

            if (length > 0) {
                for (int landID : landIDs) {
                    if (landDAO.delete(landID) == 0) {
                        errorMessages.add("There was an error deleting this land. You must delete all buildings associated with this land before deleting the land.");
                    } else {
                        landDAO.delete(landID);
                        count++;
                    }
                }
                successMessages.add(count + " lands out of " + length + " successfully deleted.");
            }
        } else {
            isHaveAction = false;
        }

        if (isHaveAction) {
            request.setAttribute("errorMessages", errorMessages);
            request.setAttribute("successMessages", successMessages);
        }

        LocationsDAO locationsDao = new LocationsDAOImpl();
        request.setAttribute("locationList", locationsDao.getList());

        LandPlansLaidDAO planDao = new LandPlansLaidDAOImpl();
        request.setAttribute("planList", planDao.getList());

        LandStatusDAO statusDao = new LandStatusDAOImpl();
        request.setAttribute("statusList", statusDao.getStatusTable());

        String baseUrl = request.getRequestURI() + "?";

        String landID = null;
        String address = null;
        int planID = -1;
        int locationID = -1;
        int areaF = -1;
        int areaT = -1;
        int costF = -1;
        int costT = -1;
        String dateF = null;
        String dateT = null;
        String status = null;

        int page = -1;

        landID = landListingForm.getId();
        landID = "".equals(landID) ? null : landID;
        if (landID != null) {
            baseUrl += "id=" + landID + "&amp;";
        }

        address = landListingForm.getAdd();
        address = "".equals(address) ? null : address;
        if (address != null) {
            baseUrl += "add=" + address + "&amp;";
        }

        planID = landListingForm.getPlan();
        if (planID > 0) {
            baseUrl += "plan=" + planID + "&amp;";
        } else {
            planID = -1;
        }

        locationID = landListingForm.getLocation();
        if (locationID > 0) {
            baseUrl += "location=" + locationID + "&amp;";
        } else {
            locationID = -1;
        }

        try {
            areaF = Integer.parseInt(landListingForm.getAreaF());
        } catch (Exception e) {
        }
        if (areaF > 0) {
            baseUrl += "areaF=" + areaF + "&amp;";
        } else {
            areaF = -1;
        }

        try {
            areaT = Integer.parseInt(landListingForm.getAreaT());
        } catch (Exception ex) {
        }
        if (areaT > 0) {
            baseUrl += "areaT=" + areaT + "&amp;";
        } else {
            areaT = -1;
        }

        try {
            costF = Integer.parseInt(landListingForm.getCostF());
        } catch (Exception ex) {
        }
        if (costF > 0) {
            baseUrl += "costF=" + costF + "&amp;";
        } else {
            costF = -1;
        }

        try {
            costT = Integer.parseInt(landListingForm.getCostT());
        } catch (Exception e) {
        }
        if (costT > 0) {
            baseUrl += "costT=" + costT + "&amp;";
        } else {
            costT = -1;
        }

        SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd");
        try {
            df.parse(landListingForm.getDateF());
            dateF = landListingForm.getDateF();
        } catch (Exception ex) {
            dateF = null;
        }
        if (dateF != null) {
            baseUrl += "dateF=" + dateF + "&amp;";
        }

        try {
            df.parse(landListingForm.getDateT());
            dateT = landListingForm.getDateT();
        } catch (Exception ex) {
            dateT = null;
        }
        if (dateT != null) {
            baseUrl += "dateT=" + dateT + "&amp;";
        }

        status = landListingForm.getStatus();
        status = "".equals(status) ? null : status;
        if (status != null) {
            baseUrl += "status=" + status + "&amp;";
        }

        try {
            int i = Integer.parseInt(request.getParameter("page"));
            page = i > 1 ? i : 1;
        } catch (Exception ex) {
        }

        Iterator<Lands> landList = landDAO.getList(landID, address, locationID, planID, areaF, areaT, costF, costT, dateF, dateT, status, page);
        request.setAttribute("landList", landList);

        PaginationUtil paginationUtil = new PaginationUtil();
        paginationUtil.setBaseUrl(baseUrl);
        paginationUtil.setNumPages(landDAO.getPageCount(landID, address, locationID, planID, areaF, areaT, costF, costT, dateF, dateT, status));
        paginationUtil.setCurPage(page);
        request.setAttribute("pagination", paginationUtil.createLinks());

        return mapping.getInputForward();
    }
}
